package com.common.base.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.common.base.tools.utils.event.BindEventBus
import com.common.base.tools.utils.event.EventBusUtils

/**
 * Created by zsf on 2022/1/20 11:49
 * *******************
 * BaseFragment
 * *******************
 */
abstract class BaseFragment<VB : ViewBinding> : Fragment(),View.OnClickListener {
    val viewBinding: VB by lazy { viewBinding() }
    protected abstract fun viewBinding(): VB
    private var isNavigationViewInit = false//记录是否已经初始化过一次视图
    private var lastView: View? = null//记录上次创建的view

    override fun onAttach(context: Context) {
        super.onAttach(context)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//        if (lastView == null) {
//            requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, callback)
//            lastView = super.onCreateView(inflater, container, savedInstanceState)
//        }
//        return lastView
        if (this.javaClass.isAnnotationPresent(BindEventBus::class.java)) {
            EventBusUtils.register(this)
        }
        return viewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (!isNavigationViewInit) {//初始化过视图则不再进行view和data初始化
            super.onViewCreated(view, savedInstanceState)
            initView(view, savedInstanceState)
            isNavigationViewInit = true
        }

    }

    override fun onPause() {
        super.onPause()
        if (!isHidden && isVisible) {
            onPagePause()
        }
    }

    override fun onResume() {
        super.onResume()
        if (!isHidden && !isVisible) {
            onPageResume()
        }
    }


    override fun onDestroyView() {
        if (this.javaClass.isAnnotationPresent(BindEventBus::class.java)) {
            EventBusUtils.unregister(this)
        }
        super.onDestroyView()
    }

    //返回按钮拦截件监听
    private val callback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
//            onBackClick()
        }
    }

    override fun onClick(v: View) {}
    protected open fun initView(view: View, savedInstanceState: Bundle?) {}

    /**
     *  Fragment 显示
     *  @see onResume
     */
    protected open fun onPageResume() {}

    /**
     *  Fragment 隐藏
     *  @see onPause
     */
    protected open fun onPagePause() {}

//    /**
//     * 监听返回事件
//     * @see callback
//     */
//    protected open fun onBackClick() {
//        if (!findNavController().navigateUp())
//            activity?.finish()
//    }
}